<template>
  <el-dialog :visible.sync="visible" :title="title"
             @closed='closedDialog'>
    <KVideoField :field="field"
                 :fieldIndex="field.index"
                 :uploadPath="videoUploadPath"
                 @upload="handleVideoUpload"
                 @del="deleteVideo"
                 @play="playVideo"/>
    <span class="dialog-footer" slot="footer">
        <el-button @click="closedDialog">取 消</el-button>
        <el-button @click="commit" type="primary">确 定</el-button>
    </span>
    </el-dialog>
</template>

<script>
import KImgField from "../knowledge/knowledgeEdit/KImgField";
import KVideoField from "../knowledge/knowledgeEdit/KVideoField";
export default {
  name: "importVideoDialog",
  components: {
    KImgField,
    KVideoField
  },
  props: {
    videoUploadPath: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      title: '上传视频',
      visible: false,
      field: {
        index: 0,
        fieldValue: {
          type: '',
          title: '',
          paths: []
                  }
              }
          }
      },
      methods:{
        playVideo() {

        },
          handleVideoUpload(fieldIndex, videoPath) {
            this.field.fieldValue.paths.push(videoPath)
          },
          deleteVideo(fieldIndex, videoIndex) {
            this.field.fieldValue.paths.splice(videoIndex, 1)
          },
          commit: function() {
            if (this.field.fieldValue.paths.length) {
              let param = new URLSearchParams();
              param.append("fieldValue", JSON.stringify(this.field.fieldValue))
              this.axios.post('/pmph/action/kindEditor/parseKnowledgeFieldVideoType', param)
                  .then((response) => {
                  this.$parent.editor.afterFocus()
                  // 如果是修改, 则清理之前的图片和图题图注;
                  if (this.title == '修改视频'){
                      let videoEl = KindEditor(this.$parent.editor.cmd.sel.focusNode).parent().first()
                      // var videoEl = parentEl.getElementsByTagName("VIDEO")
                      var cnt_doc = this.$parent.editor.cmd.doc
                      // 删除原视频标题
                      if (cnt_doc.getElementById('v_title_' + videoEl.attr("id"))) {
                        cnt_doc.getElementById('v_title_' + videoEl.attr("id")).remove()
                      }
                      videoEl.parent().html(response.data.html).remove(true)
                  }
                  else {
                    this.$parent.editor.insertHtml(response.data.html)
                  }
                  this.$parent.editor.afterBlur()
                  this.closedDialog()
                })
            }
        },
        closedDialog() {
          this.title = '上传视频'
          this.field.fieldValue.paths = []
          this.field.fieldValue.type = ''
          this.field.fieldValue.title = ''
          this.visible = false
        }
      }
    }
</script>

<style scoped>

</style>
